using Gameplay.PVE.Utils;
using UnityEngine;
using Yoozoo.Libs;

namespace Gameplay.PVE.Entity
{
    public class DropItem : IClass
    {
        public PveDropType type;
        private Vector3 position;
        private Vector3 speed;//当前速度
        private Vector3 acceleration;//加速度
        private GameObject gameObject;
        private float minHeight;
        private float stopMoveTime;
        private bool objReady = false;
        private bool isMoving = false;
        private float rotationRatio;
        private int dropId;

        void IClass.OnReset()
        {
            if (gameObject != null)
            {
                PveResourceManager.RecoverResource(gameObject,PveResourceManager.EPveResType.Drop,dropId);
            }
            gameObject = null;
            objReady = false;
            isMoving = false;
        }

        public void Update()
        {
            UpdateSpeed();
            UpdatePosition();
            gameObject.transform.localEulerAngles += new Vector3(0,60 * Time.deltaTime * rotationRatio,0);
        }

        public bool CheckIsCanPick(Vector3 position, float radius)
        {
            return TimeManager.logicTime - stopMoveTime > 0.5f && !isMoving && Vector3.Distance(position, this.position) < radius;
        }

        public void OnPick(UnitBase unit)
        {
            return;
            /*if (type == PveDropType.Money)
            {
                int money = (int)(100 * Random.Range(3,6));
                PveFlyTextManager.Instance.ShowFlyTextMoney("$ +" + money, position + new Vector3(0,Random.Range(0,0.5f),0),0.2f);
                PveManager.Instance.ShowDropMoneyFly(position);
            }
            else if (type == PveDropType.MedicBag)
            {
                if (unit is BattleUnit battleUnit)
                {
                    int hp = unit.Data.TotalHp / 2;
                    battleUnit.SetHpChange(hp);
                    PveFlyTextManager.Instance.ShowFlyTextMoney("生命恢复!", position + new Vector3(0,Random.Range(0,0.5f),0),1f);
                }
            }
            else if (type == PveDropType.BulletBag)
            {
                if (unit is BattleUnit battleUnit)
                {
                    PveFlyTextManager.Instance.ShowFlyTextMoney("攻击提升!", position + new Vector3(0,Random.Range(0,0.5f),0),1f);
                }
            }*/
        }
        
        public void Initialize(int stage, Vector3 position, PveDropType type,float range)
        {
            dropId = 0;
            if (type == PveDropType.Money)
            {
                dropId = 0;
            }
            else if (type == PveDropType.BulletBag)
            {
                dropId = 1;
            }
            else if (type == PveDropType.MedicBag)
            {
                dropId = 2;
            }
            gameObject = PveResourceManager.GetResource(PveResourceManager.EPveResType.Drop, dropId);
            rotationRatio = Random.Range(0.8f, 1.2f);
            objReady = true;
            speed = 5f * range * (new Vector3(0, 2 * Random.Range(0.7f,1.3f), 0) + new Vector3(Random.Range(-0.5f,0.5f),0,Random.Range(-0.5f,0.5f)));
            acceleration = new Vector3(0,-25f,0);
            //gameObject.transform.localScale = new Vector3(0.3f,0.05f,0.1f) * PveUtils.globalRatio;
            gameObject.transform.SetParent(PveScene.pveRootTrans);
            gameObject.transform.localEulerAngles = new Vector3(0, Random.Range(0,360f), 0);
            minHeight = 0.03f;//gameObject.transform.localScale.y / 2 + 0.03f;
            this.type = type;
            this.position = position + new Vector3(0,Random.Range(0f,PveUtils.globalRatio),0);
            if (position.y < minHeight)
            {
                this.position = new Vector3(position.x,minHeight,position.z);
            }
            isMoving = true;
            UpdatePosition();
        }

        private void UpdateSpeed()
        {
            if (!isMoving)
            {
                return;
            }
            float x = speed.x;
            float y = speed.y;
            float z = speed.z;
            x = x + acceleration.x * Time.deltaTime;
            y = y + acceleration.y * Time.deltaTime;
            z = z + acceleration.z * Time.deltaTime;
            if (x * speed.x < 0)
            {
                x = 0;
            }

            if (z * speed.z < 0)
            {
                z = 0;
            }
            
            speed = new Vector3(x,y,z);
            position = position + speed * Time.deltaTime;
            if (position.y < minHeight)
            {
                position = new Vector3(position.x,minHeight,position.z);
                isMoving = false;
                stopMoveTime = TimeManager.logicTime;
            }
           
        }
        
        private void UpdatePosition()
        {
            if (!objReady)
            {
                return;
            }
            gameObject.transform.position = position;
        }
    }
}